import { createRouter, createWebHashHistory } from "vue-router";
import Login from "../views/login/index";
import HomeView from "@/views/HomeView";
import N404 from "@/views/404";
import { userInfo, progress, routerData, menuInfo } from "@/store/pinia";
import { getRouter } from "@/router/contentRouter";
import NProgress from "nprogress"; // 进度条插件
import "nprogress/nprogress.css";
NProgress.configure({
  easing: "ease", // 动画方式
  speed: 500, // 递增进度条的速度
  showSpinner: true, // 是否显示加载 icon
  trickleSpeed: 200, // 自动递增间隔
  minimum: 0.3, // 初始化时的最小百分比
});
const routes = [
  {
    path: "/",
    name: "home",
    component: HomeView,
    children: [],
  },
  {
    path: "/login",
    name: "login",
    component: Login,
  },
  // {
  //   path: "/:catchAll(.*)",
  //   name: "404",
  //   hidden: true,
  //   component: N404,
  // },
];
const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach(async (to, from, next) => {
  const user = userInfo();
  // 显示进度条
  NProgress.start();
  if (to.path === "/login") return next();
  if (!user.isLogin) return next({ path: "login" });
  if (routerData().data.length) return next();
  // 异步路由添加
  await getRouter(router);
  next({ ...to, replace: true });
});
router.afterEach(() => NProgress.done());
export default router;
